<template>
    <div id="MedicalInsurancePolicy">

          <div class="header-container">
            
            <h1>医保政策管理内容</h1>
            <el-button type="success" class="add-button header-btn" @click="addMedicalPolicy()" size="medium">新增医保政策</el-button>
        </div>

        <el-table :data="pm.list" border style="width: 100%">

            <el-table-column fixed prop="medicalpolicy_id" label="ID" width="200">
            </el-table-column>

            <el-table-column prop="title" label="政策标题" width="350">
            </el-table-column>

            <el-table-column prop="message" label="简介" width="350">
            </el-table-column>

            <el-table-column prop="city.city_name" label="所属城市" width="250">
            </el-table-column>

            <el-table-column fixed="right" label="操作" width="150">
                <template slot-scope="scope">
                    <el-button type="primary" icon="el-icon-edit" @click="updateMedicalPolicy(scope.row)">
                    </el-button>
                    <el-button type="danger" icon="el-icon-delete" @click="deleteMedicalPolicy(scope.row.medicalpolicy_id)">
                    </el-button>
                </template>
            </el-table-column>
        </el-table>



        <el-pagination background layout="total, sizes, prev, pager, next, jumper" :total="pm.totalRecords"
            :page-size="pm.pageSize" :current-page="pm.currentPageNum" :page-sizes="[5, 10, 20, 50]"
            @size-change="sizeChange" @current-change="pageChange" style="margin-top: 20px; text-align: center;">
        </el-pagination>




        <el-dialog title="编辑医保政策" :visible.sync="updateFormvisible" :modal="false" width="350px">
            <el-form :model="medicalPolicy" :label-width="'80px'">

                <el-form-item label="政策标题">
                    <el-input v-model="medicalPolicy.title"></el-input>
                </el-form-item>

                <el-form-item label="简介">
                    <el-input v-model="medicalPolicy.message"></el-input>
                </el-form-item>


                <el-form-item label="所属城市">
                    <el-select v-model="medicalPolicy.city.city_id" placeholder="请选择所属城市">
                        <el-option v-for="item in cityList" :key="item.city_id" :label="item.city_name"
                            :value="item.city_id">
                        </el-option>
                    </el-select>
                </el-form-item>

            </el-form>
            <div slot="footer" class="dialog-footer">
                <el-button @click="cancelUpdate()">取 消</el-button>
                <el-button type="primary" @click="submmitUpdate()">提 交</el-button>
            </div>
        </el-dialog>

              <el-dialog title="新增医保政策" :visible.sync="addFormvisible" :modal="false" width="350px">
            <el-form :model="medicalPolicy" :label-width="'80px'">

                <el-form-item label="政策标题">
                    <el-input v-model="medicalPolicy.title"></el-input>
                </el-form-item>

                <el-form-item label="简介">
                    <el-input v-model="medicalPolicy.message"></el-input>
                </el-form-item>


                <el-form-item label="所属城市">
                    <el-select v-model="medicalPolicy.city.city_id" placeholder="请选择所属城市">
                        <el-option v-for="item in cityList" :key="item.city_id" :label="item.city_name"
                            :value="item.city_id">
                        </el-option>
                    </el-select>
                </el-form-item>

            </el-form>
            <div slot="footer" class="dialog-footer">
                <el-button @click="cancelAdd()">取 消</el-button>
                <el-button type="primary" @click="submmitAdd()">提 交</el-button>
            </div>
        </el-dialog>
    </div>
</template>

<script>

import axios from 'axios';

export default {
    name: "MedicalInsurancePolicy",
    data() {
        return {
            pm: {
                currentPageNum: 1,
                totalRecords: 0,
                pageSize: 5,
                totalPageNum: 0,
                startIndex: 0,
                prePageNum: 0,
                nextPageNum: 0,
                list: [],
                startPage: 0,
                endPage: 0
            },
            medicalPolicy: {
                medicalpolicy_id: null,
                title: "",
                message: "",
                city: {
                    city_id: null,
                    city_name: ""
                }
            },
            cityList: [],
            updateFormvisible: false,
            addFormvisible: false
        }
    },
    mounted() {
        this.getMedicalPolicyPage(1);
        this.getCityList();
    },
    methods: {
        getMedicalPolicyPage(e) {
            axios.get("http://localhost:8080/getMedicalPolicyPage", { params: { num: e, size: this.pm.pageSize } }).then(res => {
                this.pm = res.data;
            })
        },
        sizeChange(size) {
            this.pm.pageSize = size;
            this.getMedicalPolicyPage(1);
        },
        pageChange(num) {
            this.getMedicalPolicyPage(num);
        },
        deleteMedicalPolicy(medicalpolicy_id) {
            if (confirm("确定删除？")) {
                axios.delete("http://localhost:8080/deleteMedicalPolicy", { params: { medicalpolicy_id } }).then(res => {
                    alert(res.data)
                    this.getMedicalPolicyPage(1);
                })
            }
        },
        getCityList() {
            axios.get("http://localhost:8080/getCityList").then(res => {
                this.cityList = res.data;
            })
        },
        updateMedicalPolicy(mp) {
            this.getCityList();
            this.medicalPolicy = mp;
            this.updateFormvisible = true;
        },
        submmitUpdate() {
            axios.put("http://localhost:8080/updateMedicalPolicy", this.medicalPolicy).then(res => {
                alert(res.data);
                this.updateFormvisible = false;
                this.getMedicalPolicyPage(this.pm.currentPageNum);
                this.resetMedicalPolicy();
            })
        },
        cancelUpdate(){
            this.updateFormvisible = false;
            this.resetMedicalPolicy();
        }
        ,
        resetMedicalPolicy() {
            this.medicalPolicy = {
                medicalpolicy_id: null,
                title: "",
                message: "",
                city: {
                    city_id: null,
                    city_name: ""
                }
            }
        },
        addMedicalPolicy(){
            this.resetMedicalPolicy();
            this.addFormvisible=true;
        },
        submmitAdd(){
            axios.post("http://localhost:8080/addMedicalPolicy",this.medicalPolicy).then(res=>{
                alert(res.data);
                this.getMedicalPolicyPage(1);
                this.resetMedicalPolicy();
                this.addFormvisible=false;
            })
        },
        cancelAdd(){
            this.addFormvisible=false;
            this.resetMedicalPolicy();
        }
    }
}
</script>

<style>
#MedicalInsurancePolicy {
    width: 100%;
    height: 100%;
}
</style>